package org.example;

import com.mysql.cj.jdbc.MysqlDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/12
 * Time: 14:59
 */
public class Blog {
    public static void main(String[] args) throws SQLException {
        //1.初始化
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setServerName("jdbc:mysql://127.0.0.1:3306/java117?characterEncoding=utf8&useSSL=False");
        dataSource.setPort(3306);
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        //2.建立连接
        Connection connection = dataSource.getConnection();//需要处理SQLException
        //3.编写sql语句
        String sql = "select * from 数据表名";
        //4.预编译sql
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //5.执行sql并返回结果集
        //5.1 如果是查询使用executeQuery()方法
        //5.2如果是增删改使用executeUpdate()方法
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            System.out.println(resultSet.getString(1));
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }


}
